Passed
Push — master ( 9ca045...4164c5 )
by Maxence
02:09
created

pico_nav.createNewWebsite   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
nc 1
nop 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 4 1
1
/*
2
 * CMS Pico - Integration of Pico within your files to create websites.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: pico_elements */
28
/** global: pico_result */
29
/** global: pico_define */
30
31
var pico_nav = {
32
33
	updateNewWebsite: function (url) {
34
		pico_elements.cms_pico_new_url.text(pico_define.nchost + pico_define.sites + url);
35
		pico_nav.refreshNewFolder();
36
	},
37
38
39
	pickFolderResult: function (folder) {
40
		pico_elements.cms_pico_new_folder_result = folder;
41
		pico_nav.refreshNewFolder();
42
	},
43
44
45
	refreshNewFolder: function () {
46
		pico_elements.cms_pico_new_folder.val(pico_elements.cms_pico_new_folder_result + '/' +
47
			pico_elements.cms_pico_new_website.val());
48
	},
49
50
51
	createNewWebsite: function () {
52
53
		pico_nav.creatingWebsite(true);
54
		var data = {
55
			name: pico_elements.cms_pico_new_name.val(),
56
			website: pico_elements.cms_pico_new_website.val(),
57
			path: pico_elements.cms_pico_new_folder.val(),
58
			template: pico_elements.cms_pico_new_template.val()
59
		};
60
61
		$.ajax({
62
			method: 'PUT',
63
			url: OC.generateUrl('/apps/cms_pico/personal/website'),
64
			data: {
65
				data: data
66
			}
67
		}).done(function (result) {
68
			pico_nav.creatingWebsite(false);
69
			pico_result.createNewWebsiteResult(result);
70
		});
71
72
	},
73
74
75
	deleteWebsite: function (id, name) {
76
77
		OC.dialogs.confirm(
78
			t('cms_pico',
79
				"This operation will delete the website {name} but its content will still be available in your files", {name: name}),
80
			t('cms_pico', 'Please confirm'),
81
			function (e) {
82
				if (e === true) {
83
					$.ajax({
84
						method: 'DELETE',
85
						url: OC.generateUrl('/apps/cms_pico/personal/website'),
86
						data: {
87
							data: {
88
								id: id,
89
								name: name
90
							}
91
						}
92
					}).done(function (result) {
93
						pico_result.deleteWebsiteResult(result);
94
					});
95
96
				}
97
			});
98
	},
99
100
101
	creatingWebsite: function (creating) {
102
103
		pico_elements.cms_pico_new_submit.prop('disabled', creating);
104
		if (creating) {
105
			pico_elements.cms_pico_new_submit.val(t('cms_pico', 'Please wait'));
106
		} else {
107
			pico_elements.cms_pico_new_submit.val(t('cms_pico', 'Create a new website'));
108
		}
109
110
	},
111
112
113
	updateWebsiteOption: function (site_id, key, value) {
114
		$.ajax({
115
			method: 'POST',
116
			url: OC.generateUrl('/apps/cms_pico/personal/website/' + site_id + '/option/' + key),
117
			data: {
118
				value: value
119
			}
120
		}).done(function (res) {
121
			pico_result.displayWebsites(res.websites);
122
		});
123
	},
124
125
126
	resetFields: function () {
127
		pico_elements.cms_pico_new_website.val('');
128
		pico_elements.cms_pico_new_name.val('');
129
		pico_elements.cms_pico_new_folder.val('/');
130
		pico_elements.cms_pico_new_url.text('');
131
	},
132
133
134
	generateTmplWebsite: function (entry) {
135
		var div = $('#tmpl_website');
136
		if (!div.length) {
137
			return;
138
		}
139
140
		var tmpl = div.html();
141
142
		tmpl = tmpl.replace(/%%id%%/g, entry.id);
143
		tmpl = tmpl.replace(/%%name%%/g, escapeHTML(entry.name));
144
		tmpl = tmpl.replace(/%%address%%/g, pico_define.nchost + pico_define.sites +
145
			escapeHTML(entry.site));
146
		tmpl = tmpl.replace(/%%path%%/g, escapeHTML(entry.path));
147
148
		if (entry.options.private === '1') {
149
			tmpl = tmpl.replace(/%%private%%/g, escapeHTML(entry.options.private));
150
		}
151
152
		return tmpl;
153
	}
154
155
156
};